package com.yt.product.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.product.models.service.ICategoryInfoService;
import com.yt.product.models.vo.CategoryInfoTreeVO;
import com.yt.product.models.vo.CategoryProductInfoVO;
import com.yt.product.models.vo.ProductDetailInfoVO;
import com.yt.product.models.vo.ProductInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.product.models.service.IProductInfoService;
import com.yt.product.models.bo.ProductInfoBO;
import com.yt.product.models.dto.ProductInfoDTO;

import java.util.List;

/**
 * 产品类目关系表控制器
 *
 * @author lvzeming
 * @Date 2025-08-11 22:57:57
 */

@RestController
@RequestMapping("/api/productInfo")
@Slf4j
@Api(value = "ProductInfoApiController",tags = "产品类目关系表Api接口")
public class ProductInfoApiController {

    @DubboReference
    private IProductInfoService productInfoService;

    @DubboReference
    private ICategoryInfoService categoryInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ProductInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ProductInfoDTO dto = productInfoService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/queryProductList")
    @ApiOperation(value = "获取产品列表",notes = "获取产品列表")
    public Result<List<ProductInfoDTO>> queryProductList(ProductInfoBO bo){
        if(bo == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Wrapper<ProductInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", 0);//未删除状态
        wrapper.eq("is_publish",1);//上架状态
        if(bo.getIsNew() != null){
            wrapper.eq("is_new",bo.getIsNew());//新品上架
        }
        if(bo.getIsRecommend() != null){
            wrapper.eq("is_recommend",bo.getIsRecommend());//掌柜推荐
        }
        if(bo.getIsTreasure() != null){
            wrapper.eq("is_treasure",bo.getIsTreasure());//镇店之宝
        }
        wrapper.orderBy("update_time",false);
        wrapper.last("LIMIT 4");
        List<ProductInfoDTO> dto = productInfoService.selectList(wrapper);

        return new Result<>(dto);
    }

    @PostMapping("/queryCategoryProductList")
    @ApiOperation(value = "获取分类产品列表",notes = "获取分类产品列表")
    public Result<List<CategoryProductInfoVO>> queryCategoryProductList(){

        List<CategoryProductInfoVO> categoryProductInfoVOList = categoryInfoService.queryCategoryProductList();
        return new Result<>(categoryProductInfoVOList);
    }

    @ApiOperation(value = "产品类目关系表列表",notes = "产品类目关系表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ProductInfoDTO>> queryPageList(ProductInfoBO bo){
        Wrapper<ProductInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ProductInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = productInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }


    @PostMapping("/getProductInfoDetail")
    @ApiOperation(value = "产品详情",notes = "产品详情信息")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ProductDetailInfoVO> getProductInfoDetail(@RequestParam("id") Long id){
        //获取商品详情信息
        ProductDetailInfoVO productDetailInfoVO = productInfoService.getProductInfoDetail(id);
        return new Result<>(productDetailInfoVO);
    }
}
